//package com.yanftch.hdc_tools.insertcode;
//
//import com.intellij.openapi.actionSystem.AnAction;
//import com.intellij.openapi.actionSystem.AnActionEvent;
//import com.intellij.openapi.command.WriteCommandAction;
//import com.intellij.openapi.editor.CaretModel;
//import com.intellij.openapi.editor.Document;
//import com.intellij.openapi.editor.Editor;
//import com.intellij.openapi.project.Project;
//import com.yanftch.hdc_tools.common.MessageDelegate;
//import com.yanftch.hdc_tools.insertcode.bean.CodeBean;
//import com.yanftch.hdc_tools.insertcode.dialog.InsertCodeFrame;
//import com.yanftch.hdc_tools.insertcode.storage.InsertCodeState;
//import com.yanftch.hdc_tools.insertcode.util.FileUtils;
//import com.yanftch.hdc_tools.insertcode.util.GsonUtils;
//
//import javax.swing.*;
//import java.util.ArrayList;
//import java.util.List;
//
//import static com.yanftch.hdc_tools.common.ExtKt.mergeLists;
//
//public class InsertCodeAction extends AnAction {
//    private AnActionEvent mAnActionEvent;
//
//    @Override
//    public void actionPerformed(AnActionEvent e) {
//        mAnActionEvent = e;
////        String configPath = PathManager.getConfigPath();
////        System.out.println("日志1：" + configPath);
//
//        try {
//            new getWorker().execute();
////            worker.execute();
////            List<CodeBean> defaultList = FileUtils.getList();
////            String loadJsonFromFile = InsertCodeState.INSTANCE.loadJsonFromFile();
////            List<CodeBean> userList = com.alibaba.fastjson.JSONObject.parseArray(loadJsonFromFile, CodeBean.class);
////            List<CodeBean> realList = new ArrayList<>();
////            if (userList != null && !userList.isEmpty()) {
////                realList.addAll(userList);
////            } else {
////                realList.addAll(defaultList);
////            }
////            // 打印解析后的结果
//////            for (CodeBean code : realList) {
//////                System.out.println("模板列表：name=" + code.getName() + ", value=" + code.getValue());
//////            }
////
////            // 显示弹窗
//////            CodeDialog codeDialog = new CodeDialog(realList);
//////            codeDialog.show();
////            CodeFrame frame = new CodeFrame(realList);
////            frame.setListener(code -> {
//////                    System.out.println("选中：name=" + code.getName() + ", value=" + code.getValue());
////                boolean error = code.getValue() == null || code.getValue().isEmpty();
////                insert(e, error ? "" : code.getValue());
////            });
//        } catch (Exception exception) {
//            MessageDelegate.INSTANCE.error("出错了，掘金上联系下开发者吧~", e);
//        }
//    }
//
//    private class getWorker extends SwingWorker<List<CodeBean>, String> {
//
//        @Override
//        protected List<CodeBean> doInBackground() throws Exception {
//            List<CodeBean> result = new ArrayList();
//            try {
//                // 默认配置数据
//                List<CodeBean> defaultList = FileUtils.getList();
//                String loadJsonFromFile = InsertCodeState.INSTANCE.loadJsonFromFile();
//                // 用户配置数据
//                List<CodeBean> userList = GsonUtils.INSTANCE.jsonToList(loadJsonFromFile, CodeBean.class);
//                List<CodeBean> mergedLists = mergeLists(userList, defaultList);
//                result.addAll(mergedLists);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return result;
//        }
//
//        @Override
//        protected void done() {
//            try {
//                List<CodeBean> realList = get();
//                InsertCodeFrame frame = new InsertCodeFrame(realList);
//                frame.setListener(code -> {
////                    System.out.println("选中：name=" + code.getName() + ", value=" + code.getValue());
//                    boolean error = code.getValue() == null || code.getValue().isEmpty();
//                    insert(mAnActionEvent, error ? "" : code.getValue());
//                });
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 插入代码
//     */
//    private void insert(AnActionEvent e, String value) {
//        // 获取当前的项目和编辑器
//        Project project = e.getProject();
//        Editor editor = e.getData(com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR);
//
//        if (editor != null && project != null) {
//            WriteCommandAction.runWriteCommandAction(project, () -> {
//                // 获取 Document 和 CaretModel
//                Document document = editor.getDocument();
//                CaretModel caretModel = editor.getCaretModel();
//
//                // 获取当前光标位置
//                int offset = caretModel.getOffset();
//
//                // 插入文本：开始写入操作
//                editor.getDocument().insertString(offset, value);
//
//                // 可选：移动光标到插入文本的末尾
//                caretModel.moveToOffset(offset + value.length());
//            });
//        }
//    }
//}
